

package cn.gz3c.uitest.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;

import com.huawei.hms.audioeditor.sdk.asset.HAEAudioAsset;
import com.huawei.hms.audioeditor.sdk.asset.HAEAudioVolumeCallback;
import com.huawei.hms.audioeditor.sdk.bean.HAEAudioVolumeObject;
import com.huawei.hms.audioeditor.sdk.util.SmartLog;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import cn.gz3c.uitest.viewmodel.EditPreviewViewModel;

public abstract class BaseWaveTrackView extends BaseTrackView {
    protected final CopyOnWriteArrayList<HAEAudioVolumeObject> I = new CopyOnWriteArrayList();
    protected final List<HAEAudioVolumeObject> J = new ArrayList();
    protected List<HAEAudioVolumeObject> K;
    private final List<HAEAudioVolumeObject> L = new CopyOnWriteArrayList();
    protected BaseWaveTrackView.b M;
    protected HandlerThread N = null;
    private BaseWaveTrackView.a O;
    private long P;
    private long Q;
    private float R;
    private long S;
    private long T;
    protected float speed = 1.0F;
    protected float V = 0.0F;
    protected long W;
    protected long aa;
    private boolean ba = false;
    protected String ca = "";
    protected String da = "";
    protected int ea;

    public BaseWaveTrackView(Activity var1, EditPreviewViewModel var2, int var3) {
        super(var1, var2);
        this.ea = var3;
    }

    private void x() {
        BaseWaveTrackView.a var1;
        if ((var1 = this.O) != null) {
            var1.removeCallbacksAndMessages((Object) null);
            this.O = null;
        }

        HandlerThread var2;
        if ((var2 = this.N) != null) {
            var2.getLooper().quit();
            this.N.quit();
            this.N = null;
        }

    }

    protected void onDraw(Canvas var1) {
        if (this.ea != 1) {
            super.onDraw(var1);
        }
    }

    public void b(double var1) {
        super.z = com.huawei.hms.audioeditor.ui.common.utils.a.a(var1, 0);
        this.postInvalidate();
    }

    public void a(long var1) {
        boolean var11;
        label56:
        {
            super.g = var1;
            long var3 = (var1 = this.k()) / 2L;
            if (this.a() != null) {
                long var5 = this.a().getStartTime();
                long var7 = this.a().getEndTime();
                long var9;
                if ((var9 = super.g) + var3 >= var5 && var9 <= var7 + var3) {
                    SmartLog.d("WaveTrack", "get the screen duration is " + var1);
                    this.P = super.g - var1 - var5;
                    if (this.P < 0L) {
                        this.P = 0L;
                    }

                    SmartLog.d("WaveTrack", com.huawei.hms.audioeditor.ui.p.a.a("get the requiredStartTime is ").append(this.P).toString());
                    if (this.P == 0L) {
                        this.Q = var1 * 2L;
                    } else {
                        this.Q = var7 - super.g + var1;
                    }

                    if (this.Q > (var1 *= 2L)) {
                        this.Q = var1;
                    }

                    if (this.Q > this.a().getDuration()) {
                        this.Q = this.a().getDuration();
                    }

                    if (((HAEAudioAsset) this.a()).getAudioList().size() == 0) {
                        var11 = true;
                    } else {
                        SmartLog.d("WaveTrack", com.huawei.hms.audioeditor.ui.p.a.a("get the requiredDuration is ").append(this.Q).toString());
                        var11 = Math.abs(this.R - (float) this.P) > (float) var3 || Math.abs(this.S - this.Q) > var3 || (float) (this.W + this.P) * this.speed != this.R || this.T != var7;

                        if (var11) {
                            this.R = (float) (this.W + this.P) * this.speed;
                            this.S = this.Q;
                            this.T = var7;
                        }
                    }
                    break label56;
                }
            }

            var11 = false;
        }

        if (var11) {
            this.w();
        }

    }

    protected void onVisibilityChanged(@NonNull View var1, int var2) {
        super.onVisibilityChanged(var1, var2);
        if (var2 == 4) {
            this.x();
        }

    }

    public void a(int var1) {
        super.l = var1;
    }

    protected void w() {
        this.t();
        this.da = ((HAEAudioAsset) this.a()).getThumbNailRequestId();
        BaseWaveTrackView.b var1;
        if ((var1 = this.M) != null) {
            var1.cancel(true);
            this.M = null;
        }

        this.M = new BaseWaveTrackView.b(this.da);
        this.M.execute();
        this.ca = this.da;
    }

    public void t() {
        ((HAEAudioAsset) this.a()).interruptThumbnailGet(this.ca);
    }

    protected BaseWaveTrackView.a u() {
        if (this.N == null) {
            this.N = new HandlerThread("WaveThread");
            this.N.start();
        }

        if (this.O == null) {
            this.O = new BaseWaveTrackView.a(this.N.getLooper());
        }

        return this.O;
    }

    protected void v() {
        SmartLog.e("WaveTrack", "stopTask");
        Message var1;
        (var1 = Message.obtain()).what = 4;
        if (this.N != null) {
            this.u().sendMessage(var1);
        }

        this.ba = true;
        BaseWaveTrackView.b var2;
        if ((var2 = this.M) != null) {
            var2.cancel(true);
            this.M = null;
        }

    }

    protected CopyOnWriteArrayList<HAEAudioVolumeObject> a(CopyOnWriteArrayList<HAEAudioVolumeObject> var1, long var2, long var4) {
        if (var1 != null && var1.size() > 0) {
            CopyOnWriteArrayList var10000 = var1;
            var1 = new CopyOnWriteArrayList<>();

            for (Object value : var10000) {
                HAEAudioVolumeObject var7 = (HAEAudioVolumeObject) value;
                if (this.speed != 1.0F) {
                    long var8;
                    long var11 = var8 = (long) ((float) var7.getTime() / this.speed);
                    HAEAudioVolumeObject var10;
                    HAEAudioVolumeObject var10002 = var10 = new HAEAudioVolumeObject(var7.getTime(), var7.getVolume(), var7.getMaxValue());
                    var10002.setTime(var8);
                    if (var11 >= var2 && var8 <= var4) {
                        var1.add(var10);
                    }
                } else if (var7.getTime() >= var2 && var7.getTime() <= var4) {
                    var1.add(var7);
                }
            }

            return var1;
        } else {
            return new CopyOnWriteArrayList(Collections.EMPTY_LIST);
        }
    }

    @SuppressLint({"StaticFieldLeak"})
    protected class b extends AsyncTask<Void, Void, Void> {
        private float a = 0.0F;
        private boolean b = false;
        private long c = 0L;
        private String d;

        public b(String var2) {
            this.d = var2;
        }


        @Override
        protected Void doInBackground(Void... voids) {
            BaseWaveTrackView var6;
            long var2 = (long) ((float) ((var6 = BaseWaveTrackView.this).W + var6.P) * (var6 = BaseWaveTrackView.this).speed);
            long var4 = (long) ((float) (var6.W + var6.P + BaseWaveTrackView.this.Q) * BaseWaveTrackView.this.speed);
            SmartLog.i("WaveTrack", "GetThumbNailTask from " + var2 + ", realDuration = " + var4);
            BaseWaveTrackView.this.L.clear();
            ((HAEAudioAsset) BaseWaveTrackView.this.a()).updateVolumeObjects(this.d, BaseWaveTrackView.super.l, var2, var4, new HAEAudioVolumeCallback() {
                @Override
                public void onAudioAvailable(HAEAudioVolumeObject haeAudioVolumeObject) {

                }

                @Override
                public void onAudioEnd() {

                }
            });
            return null;
        }
    }

    protected class a extends Handler {
        private boolean a = true;

        public a(Looper var2) {
            super(var2);
        }

        public void handleMessage(@NonNull Message var1) {
            if (this.a) {
                WeakReference var2;
                if ((var2 = BaseWaveTrackView.super.G) != null) {
                    EditPreviewViewModel var8;
                    if ((var8 = (EditPreviewViewModel) var2.get()) != null) {
                        BaseWaveTrackView var9;
                        if (!var8.a(BaseWaveTrackView.this.a()) && (var9 = BaseWaveTrackView.this).M != null) {
                            var9.v();
                        } else {
                            BaseWaveTrackView var4;
                            CopyOnWriteArrayList var5;
                            BaseWaveTrackView var10001;
                            long var10004;
                            switch (var1.what) {
                                case 1:
                                    if ((var5 = ((HAEAudioAsset) BaseWaveTrackView.this.a()).getAudioList()).size() > 0) {
                                        BaseWaveTrackView.this.V = (float) ((HAEAudioVolumeObject) var5.get(0)).getMaxValue();
                                    } else {
                                        BaseWaveTrackView.this.V = 32767.0F;
                                    }

                                    if (var5.size() > 0) {
                                        var10001 = var4 = BaseWaveTrackView.this;
                                        var10004 = var4.W;
                                        var10001.K = var10001.a(var5, var10004, var10004 + var4.r);
                                        BaseWaveTrackView.this.postInvalidate();
                                    }
                                    break;
                                case 2:
                                    CopyOnWriteArrayList var10;
                                    CopyOnWriteArrayList var12 = var10 = new CopyOnWriteArrayList((List) var1.obj);
                                    if (var12.size() > 0) {
                                        ((HAEAudioAsset) BaseWaveTrackView.this.a()).setAudioList(var10);
                                        BaseWaveTrackView var6;
                                        var10001 = var6 = BaseWaveTrackView.this;
                                        var10004 = var6.W;
                                        var10001.K = var10001.a(var10, var10004, var10004 + var6.r);
                                        Iterator var7 = BaseWaveTrackView.this.K.iterator();

                                        while (var7.hasNext()) {
                                            HAEAudioVolumeObject var11 = (HAEAudioVolumeObject) var7.next();
                                            BaseWaveTrackView var3;
                                            if ((var3 = BaseWaveTrackView.this).V == 0.0F) {
                                                var3.V = (float) var11.getMaxValue();
                                            }
                                        }

                                        BaseWaveTrackView.this.postInvalidate();
                                    }
                                    break;
                                case 3:
                                    if (!(((HAEAudioAsset) BaseWaveTrackView.this.a()).isRetrieveThumbnails() ^ true) && BaseWaveTrackView.this.O != null) {
                                        this.sendEmptyMessageDelayed(3, 100L);
                                    }

                                    if ((var5 = ((HAEAudioAsset) BaseWaveTrackView.this.a()).getAudioList()) != null && var5.size() > 0) {
                                        var10001 = var4 = BaseWaveTrackView.this;
                                        var10004 = var4.W;
                                        var10001.K = var10001.a(var5, var10004, var10004 + var4.r);
                                        BaseWaveTrackView.this.postInvalidate();
                                    }
                                    break;
                                case 4:
                                    this.a = false;
                                    BaseWaveTrackView.this.x();
                            }

                        }
                    }
                }
            }
        }
    }
}
